$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'jobgroup/pageList',
        mtype: "POST",
        datatype: "json",
        colModel: [
			{ label: '排序', name: 'id', index: 'id', width: 50, key: true },
			{ label: 'AppName', name: 'appname', index: 'appname', width: 80 },
            { label: '名称', name: 'title', sortable: false, width: 75 },
			{ label: '注册方式', name: 'addressType', index: 'addressType', width: 80, formatter: function(value, options, row){
                return value === 0 ?
                    '自动注册' :
                    '手动注册';
            }},
			{ label: '在线机器地址', name: 'registryList', sortable: false, width: 80, formatter: function(value, options, row){
			        var dom = "";
			        $.each(value, function(index, item){
			            dom += '<span class="label label-success">'+item+'</span><br/>';
                    });
                    return dom;
                }},
			{ label: '操作', name: '', index: '', width: 80 }
        ],
		viewrecords: true,
        height: 'auto',
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        //width: document.body.clientWidth,根据body的宽度使用这个请注释掉autowidth:true
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "data.data",
            page: "data.pageNum",
            total: "data.pages",
            records: "data.recordCounts"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    $("#jqGrid").filterToolbar({}).jqGrid('navGrid',"#jqGridPager",{//刷新按钮
        refresh: true,
        edit:false,del: false,search:false,add:false
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        autoRegister: true,
		title: null,
        jobgroup: {
            addressList: null,
            addressType: 0
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
            vm.user = {addressType: 0};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
        changeAddressType: function(event){
            var val=$('input:radio[name="addressType"]:checked').val();
            if(val == 1){
                vm.autoRegister = false;
            }else{
                vm.autoRegister = true;
            }
        },
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.jobgroup.id == null ? "jobgroup/save" : "jobgroup/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.jobgroup),
                    success: function(r){
                        if(r.code === 200){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "jobgroup/remove",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 200){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "jobgroup/loadById?id="+id, function(r){
			    if(r.code == 200){
                    vm.jobgroup = r.content;
                }else{
			        alert(r.msg);
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
		}
	}
});